package guo.mybatis.service;

import guo.mybatis.dao.UserDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserService {
	private static final String SQL_SELECT = "select name from t_user where id=?";

	public String selectNameById(Connection connection, int id) {
		String name = null;
		try (UserDao UserDao = new UserDao(connection, SQL_SELECT)) {
			PreparedStatement statement = UserDao.getStatement();
			statement.setInt(1,id);
			ResultSet resultSet =  statement.executeQuery();
			if (resultSet.next()){
				name = resultSet.getString("name");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return name;
	}
}
